﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using robot_client.bidc;

namespace robot_client
{
    class robotManager
    {
        /// <summary>
        ///  本方法
        /// </summary>
        /// <returns></returns>
        /// 

        static System.Threading.Thread packetthread;
        static System.Threading.Thread workthread;
        static bool running;
        static List<int>  list;
        static  Socket ServerSocket;
        static IPEndPoint ServerInfo;
        static object obj;
        public static void recvpack()
        {

            byte[] buf =new byte[1024]; 


            while (robotManager.running)
            {
                Socket acc = ServerSocket.Accept();
                
                int size=acc.Receive(buf );
                if (0 == size)
                    continue;

                int i = BitConverter.ToInt32(buf,0);
                lock (robotManager.obj)
                {
                    robotManager.list.Add(i);

                }
            
            
            
            
            }
        
        
        
        
        
        }
        public static void sendwork()
        {

            while (robotManager.running)
            {


                if (0 != list.Count)
                {
                    foreach (int i in list)
                    {
                        Service1SoapClient a = new Service1SoapClient();
                       int ret=a.bid("sasa", "sasas", i.ToString());


                       if (ret < 0)
                       {
                           lock (robotManager.obj)
                            {
                                robotManager.list.Remove(i);
                            }

                       }
                    
                    
                    }
                
                
                
                
                
                
                
                
                }









            }





        }



        public static bool init()
        {
            //初始化一些变量
            robotManager.list = new List<int>();
            obj = new object();






            return true;
        }

        public static bool start()
        {  //启动侦听
          //启动工作线程
            ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //提供一个 IP 地址，指示服务器应侦听所有网络接口上的客户端活动
            IPAddress ip = IPAddress.Any;
            ServerInfo=new IPEndPoint(ip,12333);
            ServerSocket.Bind(ServerInfo);//将SOCKET接口和IP端口绑定
            ServerSocket.Listen(10);//开始监听，并且挂起数为10

            packetthread = new System.Threading.Thread(new System.Threading.ThreadStart(recvpack));

            workthread = new System.Threading.Thread(new System.Threading.ThreadStart(recvpack));




            return true;
        }

        public static void close()
        { 
           packetthread.Abort();
           workthread.Abort();
        
        
        }



    }
}
